package RadioButton;

import javax.swing.*;

public class RadioButtonControl extends JPanel {
    public JRadioButton[] radioButtons;

    public RadioButtonControl(String title,String[] item,int x,int y,JFrame RadioButton){
        JLabel jl = new JLabel(title);
        jl.setBounds(x,y,100,20);
        RadioButton.add(jl);

        this.setBounds(x+100,y,item.length*60,40);
        RadioButton.add(this);

        ButtonGroup bg = new ButtonGroup();
        radioButtons = new JRadioButton[item.length];
        for (int i=0;i<item.length;i++) {
            radioButtons[i] = new JRadioButton(item[i]);
            this.add(radioButtons[i]);
            bg.add(radioButtons[i]);
        }

        //默认选择的是第一个
        radioButtons[0].setSelected(true);
    }

    public String getItem(){
        for (JRadioButton jr:radioButtons){
            if (jr.isSelected()){
                return jr.getText();
            }
        }
        return null;
    }
}
